/**
 * 
 */
package com.dhcc.frame.lang3.reflect;

import java.lang.reflect.Field;
import java.util.List;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhcc.frame.lang3.Student;

/**
 * @author 谢进军
 *
 */
public class FieldUtilsTest {
	private Logger logger = LoggerFactory.getLogger( FieldUtilsTest.class );
	//---------获取字段
	/**
	 * 获得某个类及父类的所有字段，结果以数组形式返回
	 */
	@Test public void testGetAllFields(){
		Field[] fields = FieldUtils.getAllFields( Student.class );
		for (int i = 0; i < fields.length; i++) {
			logger.info(fields[i].getName());
		}
	}
	/**
	 * 获得某个类及父类的所有字段，结果以数组形式返回
	 */
	@Test public void testGetAllFieldsList(){
		List<Field> fields = FieldUtils.getAllFieldsList( Student.class );
		for (int i = 0; i < fields.size(); i++) {
			logger.info(fields.get(i).getName());
		}
	}
	/**
	 * 
	 */
	@Test public void testGetDeclaredField(){
		Field publicField = FieldUtils.getDeclaredField( Student.class, "xx" );
		logger.info(publicField.getName());
		Field privateField = FieldUtils.getDeclaredField( Student.class, "sex",true );
		logger.info(privateField.toString());
		logger.info(privateField.getName());
	}
	//----------获得字段的内容
	@Test public void testReadDeclaredField() throws IllegalAccessException{
		Student student = new Student();
		student.xx = "xx";
//		student.setXx( "xx");
		Object name = FieldUtils.readDeclaredField(new Student(), "publicStaticString");
		logger.info( String.valueOf( name ) );
	}
}
